package com.yazi.mxz.behavior.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.myframework.smhj.jackson.result.ListResult;
import com.yazi.mxz.behavior.common.BaseController;
import com.yazi.mxz.behavior.entity.repository.StoreGoodsAccess;
import com.yazi.mxz.behavior.jackson.filter.StoreGoodsAccessFilter;
import com.yazi.mxz.behavior.repository.StoreGoodsAccessRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.geo.Point;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("storeGoodsAccessController")
@RequestMapping("/storeGoodsAccess")
public class StoreGoodsAccessController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreGoodsAccessController.class);

	@Resource
	StoreGoodsAccessRepository storeGoodsAccessRepository;
	
	/**
	 * 获取用户最近浏览的商品
	 * @param req
	 * @param pageable
	 * @return
	 */
	@RequestMapping("/latest4ov/json") // latest for one visitor
	@ResponseBody
	public ListResult<?> latest4ovJson(HttpServletRequest req, Pageable pageable) {

		ListResult<StoreGoodsAccess> jsonResult = new ListResult<StoreGoodsAccess>();
		jsonResult.setFilter(StoreGoodsAccessFilter.getWithStoreGoodsAndStore());
		String cookieId;
		List<StoreGoodsAccess> list;
		try {
			
			cookieId = getCookie(COOKIE_KEY_USER_ID, req).getValue();
			
			log.debug("latest4ovJson: cookie={}", cookieId);
			
			// 没有cookie即没有记录
			if (null == cookieId) {
				jsonResult.setResult(new ArrayList<StoreGoodsAccess>());
				jsonResult.setOk();
				return jsonResult;
			}
			
			list = storeGoodsAccessRepository.findById_CookieOrderByAccessTimeDesc(Long.parseLong(cookieId), pageable);
			jsonResult.setResult(list);
			jsonResult.setOk();
			
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;

	}

	/**
	 * 获取附近用户最近浏览的商品
	 * @param x lng
	 * @param y lat
	 * @param pageable
	 * @return
	 */
	@RequestMapping("/latest4nv/json") // latest for near visitor
	@ResponseBody
	public ListResult<?> latest4nvJson(Double lng, Double lat, Pageable pageable) {

		ListResult<StoreGoodsAccess> jsonResult = new ListResult<StoreGoodsAccess>();
		jsonResult.setFilter(StoreGoodsAccessFilter.getWithStoreGoodsAndStore());
		List<StoreGoodsAccess> list;
		try {
			
			log.debug("latest4nvJson: [{},{}]", lng, lat);
			list = storeGoodsAccessRepository.findStoreGoodsDistinctByLocationNearOrderByAccessTimeDesc(new Point(lng, lat), pageable);
			jsonResult.setResult(list);
			jsonResult.setOk();
			
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;

	}
}
